// Recreating Rankings
use "../dta/stock_daily.dta",clear
// Defining Markets
gen markettype = int(code/10000)
gen mkt=""
replace mkt="SH" if mkt=="" & markettype==60
replace mkt="SZ" if mkt=="" & markettype==0
replace mkt="SZ" if mkt=="" & markettype==30
drop if mkt == ""

//Total Trading Volume in RMB Terms
gen volume_value = volume*price 
// Saving Base Data
keep code date mkt volume_value float_cap
tempfile temp1
save `temp1'

// Pre-Event Windows Used to Measure Input Variables
// Shanghai Exchange
local SH_time_1="mdy(10,22,2012) & date<=mdy(1,4,2013)"
local SH_time_2="mdy(6,1,2013) & date<=mdy(9,1,2013) "
local SH_time_3="mdy(6,1,2014) & date<=mdy(9,1,2014)"
// Shenzhen Exchange
local SZ_time_1="mdy(10,1,2012) & date<=mdy(1,1,2013)"
local SZ_time_2="mdy(6,1,2013) & date<=mdy(9,1,2013)"
local SZ_time_3="mdy(6,1,2014) & date<mdy(9,1,2014)"

// Relative Weights for Index Calculation
// Locals for weight
// Shanghai Exchange
local SH_w_1=1
local SH_w_2=1
local SH_w_3=2
// Shenzhen Exchange
local SZ_w_1=2
local SZ_w_2=2
local SZ_w_3=2


//Locals for Filtering
local filter_1="10_12_2012"
local filter_2="6_8_2013"
local filter_3="6_8_2014"

//Locals for previous qualifiers
local qual_1 "1st"
local qual_2 "2nd"
local qual_3 "3rd"
local qual_4 "4th"

//Creating Rankings
foreach v in 1 2 3{
	// Filtering Out Using Screening
	use ../dta/last_`filter_`v'', clear
	drop if avg_ind<1   // stocks that fail to pass rule 3.2 are dropped 
	keep code
	duplicates drop
	
	// Adding back on the full set of qualifying stocks
	local z=`v'+1
	merge 1:1 code using ../src/`qual_`z''_shsz, nogen
	keep code
	
	// Merging on Stock Characteristics
	merge 1:m code using `temp1', keep(match) nogen
	
	// Looping Across Exchanges
	foreach exchange in SH SZ{
		preserve
		keep if mkt == "`exchange'" 
		keep if date>=``exchange'_time_`v'' 
		
		// Creating Index Inputs
		sort mkt date
		bysort mkt: egen avg_floatcap_mkt=mean( float_cap)
		bysort mkt: egen avg_dollarvol_mkt=mean( volume_value )
		sort code date
		bysort code: egen avg_floatcap=mean( float_cap)
		bysort code: egen avg_dollarvol=mean( volume_value )
		
		// Computing index
		gen indic=``exchange'_w_`v''*( avg_floatcap/ avg_floatcap_mkt)+ avg_dollarvol/ avg_dollarvol_mkt
		keep code indic mkt
		duplicates drop code indic, force
		
		// Dropping Anything That Qualified in a Previous Vintage
		merge 1:1 code using ../src/`qual_`v''_shsz, keep(1) nogen
		
		// Identifying Successful Qualfiers
		merge 1:1 code using ../src/`qual_`z''_shsz, keep(match master) gen(D)
		recode D (1=0) (3=1) 
		
		//Creating Rank and Testing
		gsort -indic
		gen r=_n
		gen vintage=`v'
		gen rd_group=vintage+1
		tempfile ranking_`exchange'_`v' 
		save `ranking_`exchange'_`v'', replace
		restore
	}		
}

// Appending Rankings Across Vintages + Exchanges
clear
foreach v in 1 2 3{
	foreach exchange in SH SZ{
		append using `ranking_`exchange'_`v''
	}
}

// Setting Threshold to 0 For Each Vintage and Exchange
sum indic if mkt=="SH" & rd_group==2 & (r==166|r==167)
gen threshold_c=r(mean) if mkt=="SH" & rd_group==2
sum indic if mkt=="SH" & rd_group==3 & (r==106|r==107)
replace threshold_c=r(mean) if mkt=="SH" & rd_group==3
sum indic if mkt=="SH" & rd_group==4 & (r==109|r==110)
replace threshold_c=r(mean) if mkt=="SH" & rd_group==4

// Setting Threshold to 0 For Each Vintage and Exchange
sum indic if mkt=="SZ" & rd_group==2 & (r==113|r==114) 
replace threshold_c=r(mean) if mkt=="SZ" & rd_group==2
sum indic if mkt=="SZ" & rd_group==3 & (r==110|r==111) 
replace threshold_c=r(mean) if mkt=="SZ" & rd_group==3
sum indic if mkt=="SZ" & rd_group==4 & (r==118|r==119)
replace threshold_c=r(mean) if mkt=="SZ" & rd_group==4

// Generating Running Variables
gen running=indic-threshold_c
drop indic r threshold_c
keep if abs(running)<0.5
save ../dta/index, replace

